package com.bort.system.scheduler;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.bort.system.common.DataContainer;
import com.bort.system.controller.DeskController;
import com.bort.system.serial.SerialManager;
import javafx.application.Platform;

public class ClientRefreshScheduler {

    ScheduledExecutorService refreshExecutor;
    ScheduledExecutorService portMonitorExecutor;

    public void autoRefresh(){
        refreshExecutor = Executors.newSingleThreadScheduledExecutor();
        refreshExecutor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<Integer, DeskController> ctrlMap = DataContainer.deskCtrlMap;
                    if(ctrlMap != null){
                        Iterator<Integer> keyIter = ctrlMap.keySet().iterator();
                        while (keyIter.hasNext()){
                            Integer dataId = keyIter.next();
                            DeskController ctrl = ctrlMap.get(dataId);
                            ctrl.refreshDeskData();
                        }
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        },2,3, TimeUnit.SECONDS);
    }

    public void portStateMonitor(){

        portMonitorExecutor = Executors.newSingleThreadScheduledExecutor();
        portMonitorExecutor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    long now = System.currentTimeMillis() / 1000;
                    if(DataContainer.hearTime + 2 <= now){
                        SerialManager serialManager = SerialManager.getInstance();
                        if(serialManager.openingPort != null && serialManager.openingPort.isOpen()){
                            serialManager.openingPort.closePort();
                            serialManager.openingPort = null;
                        }
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                if(DataContainer.mainAppController != null){
                                    DataContainer.mainAppController.setConnectState();
                                }
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        },3,2,TimeUnit.SECONDS);
    }

    public void cancelAllTask() {
        try {
            if(refreshExecutor != null){
                refreshExecutor.shutdown();
            }
            if(portMonitorExecutor != null){
                portMonitorExecutor.shutdown();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
